package com.tz.yiqingapp.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tz.yiqingapp.bean.Doctor;
import com.tz.yiqingapp.bean.DoctorBean;
import com.tz.yiqingapp.config.AccessLimit;
import com.tz.yiqingapp.dto.PageDTO;
import com.tz.yiqingapp.mapper.AdminMapper;
import com.tz.yiqingapp.mapper.DoctorMapper;
import com.tz.yiqingapp.mapper.HosptialMapper;
import com.tz.yiqingapp.mapper.UserMapper;
import com.tz.yiqingapp.utils.MyPasswordEncoder;
import com.tz.yiqingapp.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin")
public class AdminDoctor {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    DoctorMapper doctorMapper;

    @Autowired
    UserMapper studentMapper;

    @Autowired
    HosptialMapper hosptialMapper;

    @AccessLimit(times = 5)
    @PostMapping("/searchDoctor")
    // @ApiOperation(value = "查询Doctor", notes = "查询Doctor名称", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> searchDoctor(@RequestBody Doctor doctor, HttpServletRequest request) {

        if(request.getSession().getAttribute("loginrole") == null ||
            !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        List<Doctor> doctorList = new ArrayList<>();

        if(doctor.getRealname() != null)
        {
            QueryWrapper<Doctor> doctorQueryWrapper = new QueryWrapper<>();
            doctorQueryWrapper.lambda().like(Doctor::getRealname,doctor.getRealname());
            doctorList.addAll(doctorMapper.selectList(doctorQueryWrapper));
        }

        List<DoctorBean> doctorBeans=  new ArrayList<>();
        for (Doctor doctor1 : doctorList) {
            DoctorBean doctorBean = new DoctorBean(doctor1);
            doctorBean.setHosptial(hosptialMapper.selectById(doctor1.getHid()));
            doctorBeans.add(doctorBean);
        }
        return ResponseUtils.success(doctorBeans);
    }

    @AccessLimit(times = 5)
    @PostMapping("/getDoctorList")
    // @ApiOperation(value = "查询Doctor", notes = "查询Doctor名称", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> getDoctorList(@RequestBody PageDTO pageDTO, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        Page<Doctor> doctorPage = new Page<>(pageDTO.getCurrentpage() , pageDTO.getPagesize());
        IPage<Doctor> doctorIPage = doctorMapper.selectPage(doctorPage , null);
        List<Doctor> doctorList = doctorIPage.getRecords();
        List<DoctorBean> doctorBeans=  new ArrayList<>();
        for (Doctor doctor1 : doctorList) {
            DoctorBean doctorBean = new DoctorBean(doctor1);
            doctorBean.setHosptial(hosptialMapper.selectById(doctor1.getHid()));
            doctorBeans.add(doctorBean);
        }
        return ResponseUtils.successlist(  doctorIPage.getTotal(),doctorIPage.getPages(),doctorIPage.getCurrent(),doctorBeans);
    }
    @AccessLimit(times = 5)
    @PostMapping("/getAllDoctor")
    // @ApiOperation(value = "查询Doctor", notes = "查询Doctor名称", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> getAllDoctor(@RequestBody PageDTO pageDTO, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        List<Doctor> doctors = doctorMapper.selectList(null);
        List<DoctorBean> doctorBeans=  new ArrayList<>();
        for (Doctor doctor1 : doctors) {
            DoctorBean doctorBean = new DoctorBean(doctor1);
            doctorBean.setHosptial(hosptialMapper.selectById(doctor1.getHid()));
            doctorBeans.add(doctorBean);
        }
        return ResponseUtils.success(doctorBeans);
    }
    @AccessLimit(times = 5)
    @PostMapping("/addDoctor")
    // @ApiOperation(value = "添加Doctor", notes = "软删除Doctor参数", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> addDoctor(@RequestBody Doctor doctor, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        QueryWrapper<Doctor> doctorQueryWrapper = new QueryWrapper<>();
        doctorQueryWrapper.or().lambda().eq(Doctor::getName,doctor.getName());
        List<Doctor> doctorList = doctorMapper.selectList(doctorQueryWrapper);
        if(doctorList.size() >0) {
            return ResponseUtils.fail("doctor exits");
        }
        doctor.setPassword(MyPasswordEncoder.doencode(doctor.getPassword()));
        doctorMapper.insert(doctor);
        return ResponseUtils.success("doctor exits");
    }


    @AccessLimit(times = 5)
    @PostMapping("/updateDoctor")
    // @ApiOperation(value = "修改Doctor信息", notes = "修改Doctor信息", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> updateDoctor(@RequestBody Doctor doctor, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        if(doctor.getPassword() != null)
            doctor.setPassword(MyPasswordEncoder.doencode(doctor.getPassword()));
        doctorMapper.updateById(doctor);
        return ResponseUtils.success(doctor);
    }

    @AccessLimit(times = 5)
    @PostMapping("/delDoctor")
    // @ApiOperation(value = "软删除Doctorid", notes = "软删除Doctor", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> delDoctor(@RequestBody Doctor deldoctor, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        Doctor doctor = doctorMapper.selectById(deldoctor.getId());
        if(doctor != null) {
            doctor.setStatus(1);
            return ResponseUtils.success("doctor delete success");
        }
        return ResponseUtils.fail("doctor delete fail");

    }
}
